#define  _CRT_SECURE_NO_WARNINGS  1

#include "string.h"
namespace wjy
{
	const size_t string::npos = -1;
	string::string(const char* str)
		:_size(strlen(str))
	{
		_capacity = _size;
		_str = new char[_size + 1];
		strcpy(_str, str);
	}

	string::string(size_t n, char ch)
		:_str(new char[n+1])
		,_size(n)
		,_capacity(n)
	{
		for (size_t i = 0; i < n; i++)
		{
			_str[i] = ch;
		}
		_str[_size] = '\0';
	}

	//string::string()
	//	:_str(new char[1]{'\0'})
	//	,_size(0)
	//	,_capacity(0)
	//{
	//}

	string::~string()
	{
		delete[]_str;
		_str = nullptr;
		_size = _capacity = 0;
	}



	//s2(s1)
	//string::string(const string& s)
	//{
	//	_str = new char[s._capacity+1];
	//	strcpy(_str,s.c_str());
	//	_size = s._size;
	//	_capacity = s._capacity;
	//}
 
	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}
	string::string(const string& s)
	{
		string tmp(s._str);
		swap(tmp);
	}

	//s1=s3
	//string& string::operator=(const string& s)
	//{
	//	if (&s != this)
	//	{
	//		delete[] _str;
	//		_str = new char[s._capacity + 1];
	//		strcpy(_str, s.c_str());
	//		_size = s._size;
	//		_capacity = s._capacity;
	//	}
	//	return *this;
	//}

	string& string::operator=(string s)
	{
		swap(s);
		return *this;
	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}

	void string::push_back(char ch)
	{
		if (_size + 1 > _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		_str[_size++] = ch;
		_str[_size] = '\0';
	}

	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (_size + len > _capacity)
		{
			size_t newcapacity = _capacity * 2;
			if (_size + len > newcapacity)
			{
				newcapacity = _size + len;
			}
			reserve(newcapacity);
		}
		strcpy(_str + _size, str);
		_size += len;
	}

	string& string::operator+=(char ch)
	{
		push_back(ch);
		return *this;
	}

	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}

	void string::insert(size_t pos, size_t n, char ch)
	{
		assert(pos <= _size);
		if (_size + n > _capacity)
		{
			size_t newcapacity = _capacity * 2;
			if (_size + n > newcapacity)
			{
				newcapacity = _size + n;
			}
			reserve(newcapacity);
		}
		size_t end =_size+n;
		while (end > pos + n - 1)
		{
			_str[end] =_str[end-n];
			--end;
		}

		for (size_t i = 0; i < n; i++)
		{
			_str[pos+i] = ch;
		}
		_size += n;

		//string tmp(n, ch);
		//insert(pos, tmp.c_str());
	}

	void string::insert(size_t pos, const char* str)
	{
		//assert(pos <= _size);
		//size_t n = strlen(str);
		//if (_size + n > _capacity)
		//{
		//	size_t newCapacity = _capacity * 2;
		//	if (_size + n > newCapacity)
		//	{
		//		newCapacity = _size + n;
		//	}
		//	reserve(newCapacity);
		//}
		//size_t end = _size + n;
		//while (end > pos + n - 1)
		//{
		//	_str[end] = _str[end - n];
		//	--end;
		//}

		//for (size_t i = 0; i < n; i++)
		//{
		//	_str[pos + i] = str[i];
		//}
		//_size += n;
		size_t n = strlen(str);
		insert(pos, n, 'x');
		for (size_t i = 0; i < n; i++)
		{
			_str[pos + i] = str[i];
		}

	}


	void string::erase(size_t pos,size_t len)
	{
		if (len >= _size - pos)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			size_t end = pos + len;
			while (end <= _size)
			{
				_str[end - len] = _str[end];
				++end;
			}
			_size -= len;
		}
	}

	size_t string::find(char ch, size_t pos)
	{
		for (size_t i = 0; i < _size; i++)
		{
			if (_str[i] == ch)
			{
				return i;
			}
		}
		return npos;
	}
	size_t string::find(const char* str, size_t pos)
	{
		const char* p = strstr(_str + pos, str);
		if (p == nullptr)
		{
			return npos;
		}
		else
		{
			return p - _str;
		}
		
	}


	string string::substr(size_t pos, size_t len)
	{
		size_t leftlen = _size - pos;
		if (leftlen < len)
		{
			len = leftlen;
		}

		string tmp;
		tmp.reserve(len);
		for (size_t i = 0; i < len; i++)
		{
			tmp += _str[pos + i];
		}
		return tmp;
		
	}


	bool string::operator==(const string& s) const
	{
		return strcmp(_str, s._str)==0;
	}
	bool string::operator!=(const string& s) const
	{
		return !(*this == s);
	}
	bool string::operator<(const string& s) const
	{
		return strcmp(_str, s._str) < 0;
	}
	bool string::operator<=(const string& s) const
	{
		return *this < s || *this == s;
	}
	bool string::operator>(const string& s) const
	{
		return !(*this <= s);
	}
	bool string::operator>=(const string& s) const
	{
		return *this > s || *this == s;
	}


	ostream& operator<<(ostream& out, const string& s)
	{
		for (auto ch: s)
		{
			out << ch;
		}
		return out;
	}

	//istream& operator>>(istream& in, string& s)
	//{
	//	s.clear();
	//	char ch=in.get();
	//	while (ch != ' ' && ch != '\n')
	//	{
	//		s += ch;
	//		ch=in.get();
	//	}
	//	return in;
	//}

	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		const size_t N = 1024;
		char buff[N];
		int i = 0;
		char ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == N - 1)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
		}
		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}
		return in;
	}

	istream& getline(istream& is, string& s, char delim)
	{
		s.clear();
		const size_t N = 1024;
		char buff[N];
		int i = 0;
		char ch = is.get();
		while (ch != delim)
		{
			buff[i++] = ch;
			if (i == N - 1)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			ch = is.get();
		}
		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}
		return is;
	}

	void  test_string1()
	{
		//string s1;
		//cout << s1 << endl;
		//string s2("hello world");
		//cout << s2 << endl;

		//string s1,s2;
		//cin >> s1 >> s2;
		//cout << s1<< endl;
		//cout << s2 << endl;

		//string s1("hello world");
		//string s3;
		//cin >> s3;
		//cout << s3 << endl;


		//cin >> s1;
		//cout << s1 << endl;


		string s4;
		cin >> s4;
		cout << s4 << endl;

	}

	void test_string2()
	{
		string s1(5, 'x');
		s1.insert(2, 2, 'y');
		cout << s1 << endl;

		s1.erase();
		cout << s1 << endl;
        
		string s2("hello world");
		s2.insert(6, "yyyy");
		cout << s2 << endl;

		s2.erase(0, 5);
		cout << s2 << endl;
	}

	void test_string3()
	{
		string s1("hello world");
		string s2 = s1;
		cout << s2 << endl;
		string s3("hahahaha");
		s1 = s3;
		cout << s1 << endl;
	}
}
